package cn.wolfcode.web.controller;


import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.feign.AliPayFeign;
import cn.wolfcode.mq.OrderMessage;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;


@RestController
@RequestMapping("/orderPay")
@RefreshScope
public class OrderPayController {
    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    private AliPayFeign aliPayFeign;
    @Value("${pay.frontEndPayUrl}")
    private String frontEndPayUrl;

    @RequestMapping("/alipay")
    public Result<String> alipay(String orderNo, int type) {
        //数据校验
        if (StringUtils.isEmpty(orderNo) || StringUtils.isEmpty(type + "")) {
            throw new BusinessException(SeckillCodeMsg.OPT_SECKILL);
        }
        //判断是否是在线支付
        if (OrderInfo.PAYTYPE_ONLINE.equals(type)) {
            String formString = orderInfoService.payOnline(orderNo, type);
            return Result.success(formString);
        } else if (OrderInfo.PAYTYPE_INTERGRAL.equals(type)) {
            orderInfoService.payIntegral(orderNo,type);
        } else {
            throw new BusinessException(SeckillCodeMsg.OPT_SECKILL);
        }
        return Result.success("");
    }

    @RequestMapping("/return_url")
    public void return_url(@RequestParam Map<String, String> params, HttpServletResponse response) throws IOException {
        System.out.println("调用数据时间为" + new Date());
        //获取到支付宝穿件来的参数
        Result<Boolean> result = aliPayFeign.checkSign(params);
        //做验签操作
        if (result != null && !result.hasError()) {
            if (result.getData()) {
                response.sendRedirect(frontEndPayUrl + params.get("out_trade_no"));
            } else {
                response.sendRedirect("http//localhost/50x.html");
            }
        }
    }

    @RequestMapping("/notify_url")
    public void notifyUrl(@RequestParam Map<String, String> params, HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();
        Result<Boolean> result = aliPayFeign.checkSign(params);
        if (result != null && !result.hasError()) {
            if (result.getData()) {
                orderInfoService.paySuccess(params);
                writer.write("success");
            } else {
                writer.write("fail");
            }
        }
    }
    @RequestMapping("/refund")
    public Result<String> refund(String orderNo){
        //数据校验
        if (StringUtils.isEmpty(orderNo)){
            throw new BusinessException(SeckillCodeMsg.OPT_SECKILL);
        }
        //获取到orderinfo对象
        OrderInfo orderInfo = orderInfoService.getByOrderNo(orderNo);
        //判断是否线上支付
        if (orderInfo.getPayType().equals(OrderInfo.PAYTYPE_ONLINE)){
            //线上支付走退款流程
            orderInfoService.refundOnline(orderNo);

        }else if (orderInfo.getPayType().equals(OrderInfo.PAYTYPE_INTERGRAL)){
            //积分退款
            orderInfoService.refundIntegral(orderNo);
        }else{
            throw new BusinessException(SeckillCodeMsg.OPT_SECKILL);
        }
        return Result.success("");
    }
}
